using System;
using System.Data;
using System.Xml; 
using System.Collections ;
using System.IO;
using System.Diagnostics;
using System.Security;
using Microsoft.SharePoint;
using SharePoint.ImportExport.Core;
namespace SharePoint2003Import
{
	/// <summary>
	/// The class is extends the DocumentVersion class by adding a collection of versions
	/// </summary>
	internal class Document : DocumentVersion,IDisposable
	{
		private ArrayList versions = null;

		/// <summary>
		/// Destructor of the class Document
		/// </summary>
		~Document()
		{
			if( null != versions)
				versions.Clear();
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="item">This is the item class representing a a XML node from the items.xml file</param>
		/// <param name="folderName"></param>
		public Document(Item item,string folderName): base(item,folderName)
		{
			Folder=folderName;
			
			//Initial capacity defaults to 16
			this.versions = new ArrayList(); 
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="page">This is the ghosted page</param>
		/// <param name="folderName"></param>
		public Document(Page page,string folderName): base(page,folderName)
		{
			Folder=folderName;
			
			//Initial capacity defaults to 16
			this.versions = new ArrayList(); 
		}
		/// <summary>
		/// This is the enumerator for the document versions
		/// </summary>
		public IEnumerator Versions
		{
			get {return versions.GetEnumerator() ;}
		}
		/// <summary>
		/// This function adds a new version of the document.
		/// It validates that the version has the same name and that it is created later than the original one
		/// </summary>
		/// <param name="version"></param>
		/// <returns></returns>
		private bool AddVersion(DocumentVersion version)
		{
			if(null == version)
			{
				return false;
			}

			//ModifiedOn is less than CreatedOn
			if(TimeSpan.Zero > version.Modified.Subtract(this.Created))
			{
				Logger.Log(ErrorLevel.Error,"Cannot add version to file reason: the Modified date is before the Created date");
				return false;
			}

			if(!version.Name.Equals(this.Name))
			{
				Logger.Log(ErrorLevel.Error,"Cannot add version to file: Names do not match. A version has to have the same name");
				return false;
			}

			this.versions.Add(version); 
			return true;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="folderPath"></param>
		/// <param name="enableVersions"></param>
		/// <returns></returns>
        public bool ProcessData(string folderPath, bool enableVersions, Microsoft.SharePoint.SPWeb web)
		{
			bool returnValue=base.ProcessData(folderPath,web);			
			if(returnValue && enableVersions)
			{
				foreach(Item version in ThisItem.Versions)
					{
						DocumentVersion DocVersion = new DocumentVersion(version,folderPath);
						if(DocVersion.ProcessData(folderPath,web))
							this.AddVersion(DocVersion);
					}
			}
			return returnValue;


		}
		/// <summary>
		/// Helper function for renaming a file and its versions
		/// </summary>
		/// <param name="newName"></param>
		/// <returns></returns>
		public bool FixName(string newName)
		{
			Name=newName;
			bool returnValue=true;
			for(int i=0;i<this.versions.Count;i++)
			{
				DocumentVersion v=(DocumentVersion)this.versions[i];
				v.Name=newName;
			}
			return returnValue;

		}

		/// <summary>
		/// This functions reads the portal listings information from the XML node
		/// </summary>
		/// <param name="url">This is the URL to the document.</param>
		/// <returns></returns>
		public bool GetCategories(string url)
		{
			bool returnValue=true;
			foreach(Category category in ThisItem.Categories)
				{
					if(Importer.CategoriesDS == null)
					{
						Importer.CategoriesDS = new System.Data.DataSet("Categories");
						Importer.CategoriesDS.Locale=new System.Globalization.CultureInfo(Convert.ToInt16(Importer.lcid));

						DataTable dt = Importer.CategoriesDS.Tables.Add("Import");
						dt.Columns.Add("Category",typeof(String));
						dt.Columns.Add("URL",typeof(String));
						dt.Columns.Add("Title",typeof(String));
					}
					DataTable tbl=Importer.CategoriesDS.Tables["Import"];
					DataRow workRow = tbl.NewRow();
					workRow[0] = category.Value;
					workRow[1] = url;
					workRow[2] = this.Name;
					tbl.Rows.Add(workRow);



				}

			return returnValue;

		}
		#region IDisposable Members
		/// <summary>
		/// 
		/// </summary>
		public new void Dispose()
		{
			if( null != versions)
				versions.Clear();
			versions=null;
			base.Dispose();
		}

		#endregion
	}
}
